package com.petcommunity.dao;

import com.petcommunity.model.Notice;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NoticeDAO {

    public void addNotice(Notice notice) {
        String sql = "INSERT INTO notices (title, content) VALUES (?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, notice.getTitle());
            stmt.setString(2, notice.getContent());
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Notice> getAllNotices() {
        List<Notice> list = new ArrayList<>();
        String sql = "SELECT * FROM notices ORDER BY created_at ASC";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Notice notice = new Notice();
                notice.setNoticeId(rs.getInt("notice_id"));
                notice.setTitle(rs.getString("title"));
                notice.setContent(rs.getString("content"));
                notice.setCreatedAt(rs.getTimestamp("created_at"));
                list.add(notice);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Notice getNoticeById(int id) {
        Notice notice = null;
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM notices WHERE notice_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                notice = new Notice();
                notice.setNoticeId(rs.getInt("notice_id"));
                notice.setTitle(rs.getString("title"));
                notice.setContent(rs.getString("content"));
                notice.setCreatedAt(rs.getTimestamp("created_at"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return notice;
    }

    public void updateNotice(Notice notice) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE notices SET title=?, content=?, publish_date=? WHERE notice_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, notice.getTitle());
            ps.setString(2, notice.getContent());
            ps.setDate(3, (Date) notice.getCreatedAt());
            ps.setInt(4, notice.getNoticeId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteNotice(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM notices WHERE notice_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
